package test;

import org.apache.commons.httpclient.HttpConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Scanner;


/**
 * Created by Zhangyu on 2014/7/30.
 */
public class TestGetContent {
    static String url;

    static  String getlistFromUrl(String keyword) {
        String list = "";
        int page;
        try {

            url = "http://cd.meituan.com/s/" + keyword;
            URL path = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) path.openConnection();

            connection.setConnectTimeout(30000);
            connection.setReadTimeout(30000);
            connection.setRequestProperty("User-agent", "Mozilla/5.0");
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
            connection.setRequestProperty("Referer", "http://cd.meituan.com");
            connection.setRequestProperty("Cache-Control", "max-age=5");
            connection.setRequestProperty("Accept", "text/html,application/xtml+xml," +
                    "application/xml;q=0.9,*/*;q=0.8");


            connection.connect();
            InputStream urlStream = connection.getInputStream();
            BufferedReader br;
            br = new BufferedReader(new InputStreamReader(urlStream, "UTF-8"));
            char[] buf = new char[2048];
            int n = 0;
            while ((n = br.read(buf, 0, 2048)) != -1) {
                list += new String(buf, 0, 2048);
            }
            br.close();
            urlStream.close();
/**
 int index = list.indexOf("</a></li><li class=\"next\">") - 2;
 String pg = list.substring(index, list.indexOf("</a></li><li class=\"next\">"));
 if (pg.contains(">"))
 pg = pg.substring(pg.indexOf(">") + 1);
 page = Integer.parseInt(pg);
 System.out.println("一共有" + page + "页");
 **/
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return list;
    }


    public static void main(String args[]) {


//      System.out.print(list);

        System.out.println("输入你要搜索的关键字：");
        Scanner scanner = new Scanner(System.in);
        String keyword = scanner.nextLine();
        String list = getlistFromUrl(keyword);
        int page;
        int index = list.indexOf("</a></li><li class=\"next\">") - 2;
        String pg = list.substring(index, list.indexOf("</a></li><li class=\"next\">"));
        if (pg.contains(">"))
            pg = pg.substring(pg.indexOf(">") + 1);
        page = Integer.parseInt(pg);
        int amounts = 0;
        for (int j = 1; j <= page; j++) {
            String keyword1 = keyword+"/page"+j;
            list = getlistFromUrl(keyword1);
            if (!list.equals("")) {
                Document doc = Jsoup.parse(list);

                Elements titles = doc.getElementsByClass("deal-tile__title");
                Elements details = doc.getElementsByClass("deal-tile__detail");

                for (int i = 0; i < (titles.size() < details.size() ? titles.size() : details.size()); i++) {
                    String title = titles.get(i).select("a[title]").text();
                    Double price;
                    try {
                        price = Double.parseDouble(details.get(i).select("strong").text());
                    }catch(Exception e){
                        price = 0.0;
                    }
                    System.out.println(title + "\t" + price + "元");
                    amounts++;
                }

            } else
                System.err.println("爬取失败");
        }
        System.out.println("共爬取"+amounts+"条数据");
    }




}


//新品：http://cd.meituan.com/s/keyword/new
//按销量：http://cd.meituan.com/s/keyword/hot
//按价格：http://cd.meituan.com/s/keyword/price-asc
//按评分：http://cd.meituan.com/s/keyword/rating
//按时间：http://cd.meituan.com/s/keyword/time